package com.ccrego.haiyi.demo.httpdemo;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ccrego.haiyi.R;
import com.pachong.android.baseuicomponent.activity.PullableListActivity;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.framework.httprequest.ResponseParser;
import com.pachong.android.framework.httprequest.responsecallbacks.ListDataRequestListener;
import com.pachong.android.frameworkbase.utils.io.AssetsUtil;

import java.util.List;


/**
 * 刷列表的模板
 * <p>
 * 主要分为三步：fragment  + adapter(+ layout布局)  + bean
 * <p>
 * 注意事项:
 * 1. IHttpRequest request = new HttpRequest(this, mRules);
 * MyResponseCallback cb = new MyResponseCallback(this, mRules);
 * 这两个要记得带上mRules
 * <p>
 * 2. adapter要设置 泛型参数（LoginResponce）  e.g extends RecyclerViewBaseAdapter<LoginResponce>
 */
public class RequestDemoAssetListActivity extends PullableListActivity {

    //    private final static String URL_TEST = "http://hpapi.baoruan.com:10081/test/list";
    private final static String URL_TEST = "http://apitest.baoruan.com/launcher/default/index?version=9.150320&un=239BA6301606E596F70AC1E7B4808B23&name=recommendlist&channelId=ceshi_baoruanservice1";

    private int mPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        mPage = 0;
        clearDecorations();
        setPullToRefreshEnable(false);
    }

    @Override
    public void onResume() {
        super.onResume();
        startLoading();
    }

    private ListDataRequestListener<TestBean> mResponceListener = new ListDataRequestListener<>(this, TestBean.class);

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new TemplateListAdapter(this);
    }

    @Override
    public void onStartLoading() {

        String responseStr = AssetsUtil.getStringFromAssets(this, "list_data.txt");
        ResponseParser parser = new ResponseParser(TestBean.class);
        parser.setJsonString(responseStr);
        List<TestBean> list = (List<TestBean>) parser.getData();
        if (list != null && list.size() > 0) {
            getAdapter().getData().addAll(list);
        }

    }


    /**
     * Adapter
     */
    public class TemplateListAdapter extends RecyclerViewBaseAdapter<TestBean> {

        public TemplateListAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup viewGroup, int i) {
            return new ItemViewHolder(setAdapterViewItem(viewGroup, R.layout.listitem_template));
        }


        @Override
        public void onBindHolder(RecyclerView.ViewHolder viewHolder, int i) {
            ItemViewHolder holder = (ItemViewHolder) viewHolder;
            final TestBean bean = getData().get(i);
            holder.tvText.setText(bean.getName());
        }

        public class ItemViewHolder extends RecyclerView.ViewHolder {
            public RelativeLayout lytMain;
            public TextView tvText;
            public ImageView ivIcon;

            public ItemViewHolder(View itemView) {
                super(itemView);
                lytMain = (RelativeLayout) itemView.findViewById(R.id.lytMain);
                ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon);
                tvText = (TextView) itemView.findViewById(R.id.tvText);
            }
        }
    }

    public class TestBean {

        /**
         * id : 334
         * price : 333.336
         * name : nobody
         * age : 16
         */

        private int id;
        private double price;
        private String name;
        private int age;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public double getPrice() {
            return price;
        }

        public void setPrice(double price) {
            this.price = price;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }


}
